//
//  Restaurant.swift
//  SwiftRequest
//
//  Created by Pro on 2020/2/11.
//  Copyright © 2020 Pro. All rights reserved.
//

import Foundation

struct RestanurantTotal {
    let status:Int
    let message:String
    var data:Array<Any>
    let count:Int
    let request_id:String
    
    init?(data: Any) {

        guard let total = data as? Dictionary<String, Any> else {
        return nil
    }
        
    guard let status = total["status"] as? Int else {
             return nil
         }
        self.status = status
        
        guard let message = total["message"] as? String else {
                 return nil
             }
            self.message = message
        
        guard let count = total["count"] as? Int else {
                 return nil
             }
            self.count = count
        
        guard let request_id = total["request_id"] as? String else {
                 return nil
             }
        self.request_id = request_id
        
        
        guard let restaurant = total["data"] as? Array<Dictionary<String, Any>> else {
                      return nil
        }
        
        let array:NSMutableArray = NSMutableArray.init()
        
        for index in 0..<restaurant.count  {
            let dic = restaurant[index]
            let res : Restaurant = Restaurant.init(info: dic)!
            array.add(res)
        }
        self.data = array as! Array<Any>
}
    
}
    
    
    
    
    
    


struct Restaurant {

    let id : String
    let title: String
    let address: String
    let category: String
    let type: Int
    let location: LLocation
    let adcode: Int
    let province: String
    let district: String


    init?(info: Any) {

        guard let restaurant = info as? Dictionary<String, Any> else {
            return nil
        }
        guard let id = restaurant["id"] as? String else {
            return nil
        }
        guard let title = restaurant["title"] as? String else {
             return nil
         }
        guard let address = restaurant["address"] as? String else {
            return nil
        }
        guard let category = restaurant["category"] as? String else {
            return nil
        }
        guard let type = restaurant["type"] as? Int else {
            return nil
        }
        guard let adcode = restaurant["adcode"] as? Int else {
            return nil
        }
        guard let province = restaurant["province"] as? String else {
                  return nil
        }
        guard let district = restaurant["district"] as? String else {
                  return nil
        }
        guard let  location = restaurant["location"] as? Dictionary<String, Any> else {
                  return nil
        }

        self.id = id
        self.title = title
        self.address = address
        self.category = category
        self.type = type
        self.adcode = adcode
        self.province = province
        self.district = district
        self.location = LLocation.init(data: (location ))!
        
    }
}

struct  LLocation {
    let lat: Double
    let lng: Double
    
    init?(data: Any) {

        guard let location = data as? Dictionary<String, Any> else {
            return nil
        }
        guard let lat = location["lat"] as? Double else {
            return nil
        }

        guard let lng = location["lng"] as? Double else {
            return nil
        }
        self.lat = lat
        self.lng = lng
}
    
}
